package com.hm.cms.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hm.cms.auto.SystemEnvConfigProperties;
import com.hm.cms.auto.su.R.SystemConfig;
import com.hm.cms.def.AdvertSiteEnum;
import com.hm.cms.def.ExtLlinkEnum;
import com.hm.cms.service.AdvertService;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.ExtLinkService;
import com.hm.cms.service.TermTaxonomyService;
import com.hm.cms.vo.HcmsHomePageVo;
import com.hm.cms.vo.HcmsHomePageVo.WebPageNavVo;
import com.hm.cms.vo.HcmsHomePageVo.WebPageTagNavVo;
import com.hm.cms.vo.HcmsPageAdvertVo;
import com.hm.cms.vo.HcmsPageBaseInfoVo;
import com.hm.cms.vo.HcmsPageExtLinkVo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.util.EncryptUtil.Base64;

/**
 * @author shishun.wang
 * @date 上午11:43:00 2017年10月13日
 * @version 1.0
 * @describe
 */
@Controller
public class EntranceController {

	@Autowired
	private AdvertService advertService;

	@Autowired
	private ExtLinkService extLinkService;

	@Autowired
	private TermTaxonomyService termTaxonomyService;

	@Autowired
	private ArticleService articleService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public String index(Model model) {
		this.initBaseInfo(model);
		this.setHeadPageAdvert(AdvertSiteEnum.HOME_HEAD_SLIDE, model);
		this.setExtLink(model);

		HcmsHomePageVo homePageVo = new HcmsHomePageVo();
		{
			List<WebPageNavVo> pageNavs = termTaxonomyService.loadSysNavs();
			homePageVo.setWebPageNavs(pageNavs);

			List<WebPageTagNavVo> webPageTagNavs = new ArrayList<WebPageTagNavVo>();
			for (WebPageNavVo pageNav : termTaxonomyService.loadSysTermTaxos()) {
				WebPageTagNavVo vo = new WebPageTagNavVo();
				{
					vo.setTagNavName(pageNav.getName());
					vo.setShowMoreUri(pageNav.getUri());
					vo.tagNavChild(articleService.queryArticle(new PageInfoParam(1, 8), pageNav.getId(), null));
				}

				webPageTagNavs.add(vo);
			}

			homePageVo.setWebPageTagNavs(webPageTagNavs);
		}
		model.addAttribute("homePageInfo", homePageVo);
		return "index";
	}


	private void initBaseInfo(Model model) {
		HcmsPageBaseInfoVo baseInfoVo = new HcmsPageBaseInfoVo();
		{
			baseInfoVo.setOpenLogin(systemEnvConfigProperties.isOpenLogin());
			baseInfoVo.setSiteFtInfo(new String(Base64.decode(systemEnvConfigProperties.getSiteFooterInfo().toCharArray())));
			baseInfoVo.setSiteShare(new String(Base64.decode(systemEnvConfigProperties.getSiteShare().toCharArray())));
			baseInfoVo.termTaxonomy(termTaxonomyService.loadHcmsTermTaxonomys());

			baseInfoVo.hotArticle(articleService.queryArticle(new PageInfoParam(1, SystemConfig.TAG_SHOW_SIZE), null, null));
		}

		model.addAttribute("baseInfo", baseInfoVo);
	}

	private void setExtLink(Model model) {
		model.addAttribute("pageExtLlink", new HcmsPageExtLinkVo().friendshipLink(extLinkService.loadExtLinks(ExtLlinkEnum.FRIENDSHIP_LINK))
				.onlineToolLink(extLinkService.loadExtLinks(ExtLlinkEnum.ONLINE_TOOL_LINK)));
	}

	private void setHeadPageAdvert(AdvertSiteEnum advertSite, Model model) {
		model.addAttribute("pageAdvert", new HcmsPageAdvertVo().headSlides(advertService.loadPageAdvert(advertSite)));
	}
}
